package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Project;
import com.example.demo.mapper.ProjectMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Resource//此处练手项目为了快速实现效果，直接把UserMapper引入到UserController，
    // 更规范的引入应UserMapper引入到Service类，UserController通过引入service类从而引入UserMapper
    ProjectMapper projectMapper;

    @PostMapping//添加用户
    public Result<?> save(@RequestBody Project project) {
        projectMapper.insert(project);
        return Result.success();
    }

    @PutMapping//修改
    public Result<?> Update(@RequestBody Project project) {
        projectMapper.updateById(project);
        return Result.success();
    }

    @DeleteMapping("/{id}")//删除
    public Result<?> Update(@PathVariable Long id) {
        projectMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping//分页查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Project> wrapper = Wrappers.<Project>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Project::getProjectname, search);
        }
        Page<Project> userPage = projectMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }
}

